package cn.itcast.stream;

import cn.itcast.entity.UserEntity;

import java.util.ArrayList;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;

public class sum {

    public static void main(String[] args) {
        Stream<Integer> integerStream = Stream.of(10, 50, 30, 10);
        Optional<Integer> reduce = integerStream.reduce(new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer a1, Integer a2) {
                return a1 + a2;
            }
        });
        System.out.println(reduce.get());


        ArrayList<UserEntity> userEntities = new ArrayList<>();
        userEntities.add(new UserEntity("张三", 18));
        userEntities.add(new UserEntity("李四", 26));
        userEntities.add(new UserEntity("王二", 10));
        userEntities.add(new UserEntity("麻子", 28));
        Stream<UserEntity> stream = userEntities.stream();

        Optional  sum1=  stream.reduce(new BinaryOperator<UserEntity>() {
            @Override
            public UserEntity apply(UserEntity user1, UserEntity user2) {
                UserEntity userEntity1 = new UserEntity("sum",user1.getAge()+user2.getAge());
                return userEntity1;
            }
        });
        System.out.println(sum1.get());
    }
}
